//
//  KYLeftViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/11.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYLeftViewController.h"
#import "KYLeftView.h"
#import "KYLoginViewController.h"
#import "KYRegisterViewController.h"
#import "MMDrawerController.h"
#import "UIViewController+MMDrawerController.h"
#import "KYLoginModel.h"
#import "KYInformationView.h"
#import "KYFillInfoViewController.h"
#import "KYInformationView.h"
#import "Header.h"
#import "KYLeftViewCell.h"
#import "KYGroupModel.h"
#import "KYLeftLIstModel.h"

#import "KYApplyViewController.h"
#import "KYMedicalManagerViewController.h"
#import "KYDoctorAttentionViewController.h"
#import "KYSettingHelpViewController.h"
#import "KYAboutProductViewController.h"



@interface KYLeftViewController ()<KYLeftViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSArray *leftList;
@end

@implementation KYLeftViewController
-(NSArray *)leftList{
    if (!_leftList) {
        _leftList = [KYGroupModel getModel];
    }
    return _leftList;
}

//如果没有登录,就加载自定义的登录视图,如果登陆了,就加载自己的视图
-(void)loadView{
    KYLoginModel *model = [KYLoginModel readData];
    if (model.isLogin) {
        [super loadView];
        [self setUpUI];
    }else{
        KYLeftView *leftView = [[KYLeftView alloc]init];
        self.view = leftView;
        leftView.delegate = self;
    }
}
- (void)viewDidLoad {

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hasLogin:) name:@"login" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(topViewKlick:) name:@"topViewKlick" object:nil];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - 设置界面
-(void)setUpUI{
    self.view.backgroundColor = [UIColor whiteColor];
    KYInformationView *infoView = [[KYInformationView alloc]init];
    [self.view addSubview:infoView];
    infoView.frame = CGRectMake(0, 0, ScreenWith, 100);
    UITableView *leftTableView = [[UITableView alloc]init];
    CGFloat tabVY = CGRectGetMaxY(infoView.frame);
    leftTableView.frame = CGRectMake(0, tabVY, self.view.bounds.size.width, ScreenHeigth-tabVY);
    [self.view addSubview:leftTableView];
    leftTableView.dataSource = self;
    leftTableView.delegate = self;
    leftTableView.scrollEnabled = NO;
}
#pragma 登录成功的通知方法
-(void)hasLogin:(NSNotification *)notification{
    [self loadView];
}
#pragma mark - 未登录时的按钮点击代理事件
-(void)loginBtnKlick:(KYLeftView *)leftView{
    KYLoginViewController *loginVC = [[KYLoginViewController alloc]init];
    loginVC.title = @"用户登录";
    [self.mm_drawerController closeDrawerAnimated:YES TempVc:loginVC completion:nil];

}
-(void)registBtnKlick:(KYLeftView *)leftView{
    KYRegisterViewController *registerVC = [[KYRegisterViewController    alloc]init];
    registerVC.title = @"用户注册";
    [self.mm_drawerController closeDrawerAnimated:YES TempVc:registerVC completion:nil];
}
#pragma mark - 登录以后的事件
-(void)topViewKlick:(NSNotification *)notification{
    KYFillInfoViewController *fillVC = [[KYFillInfoViewController alloc]init];
    fillVC.title = @"填写实名信息";
    [self.mm_drawerController closeDrawerAnimated:YES TempVc:fillVC completion:nil];
}
#pragma mark - tableView的数据源和代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.leftList.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    KYGroupModel *group = self.leftList[section];
    return group.section.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *leftCellID = @"KYleftCellID";
    KYLeftViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftCellID];
    if (cell == nil) {
        cell = [[KYLeftViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:leftCellID];
    }
    KYGroupModel *group = self.leftList[indexPath.section];
    KYLeftLIstModel *list = group.section[indexPath.row];
    cell.name = list.name;
    cell.backgroundColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 20;
    }else{
        return 350;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    KYBasicViewController *destinationVC = nil;
    KYGroupModel *group = self.leftList[indexPath.section];
    KYLeftLIstModel *model = group.section[indexPath.row];
    if (model.type == 5) {
        return;
    }
    switch (model.type) {
        case 0:
            destinationVC = [[KYApplyViewController alloc]init];
            break;
        case 1:
            destinationVC = [[KYMedicalManagerViewController alloc]init];
        case 2:
            destinationVC = [[KYDoctorAttentionViewController alloc]init];
        case 3:
            destinationVC = [[KYSettingHelpViewController alloc]init];
        case 4:
            destinationVC = [[KYAboutProductViewController alloc]init];
        default:
            break;
    }
    destinationVC.title = model.name;
    [self.mm_drawerController closeDrawerAnimated:YES TempVc:destinationVC completion:nil];

}
@end
